// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.assets.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.assets.controller.request.ImportAssetsReq;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsForecastReq;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsOrderReq;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsForecastReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsLockReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsOrderReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsSourceReq;
import com.liuxinlong.modules.assets.service.AssetsLockService;
import com.liuxinlong.modules.assets.service.AssetsOrderService;
import com.liuxinlong.modules.assets.service.AssetsService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Assets;
import com.liuxinlong.modules.entity.AssetsMonthDetail;
import com.liuxinlong.modules.entity.AssetsSourceRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 固资管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-10-30
 */
@RestController
@RequestMapping("/assets")
public class AssetsController {
    @Autowired
    private AssetsService assetsService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private AssetsOrderService assetsOrderService;

    @Autowired
    private AssetsLockService assetsLockService;


    /**
     * 分页查询固资管理信息列表
     *
     * @param req 请求信息
     * @return 固资管理信息列表
     */
    @PostMapping("page")
    public BaseResp pageAssetsList(@RequestBody QueryAssetsReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("year", req.getCurrentYear());
        queryMap.put("department", req.getDepartment());
        queryMap.put("projectManage", req.getProjectManage());
        queryMap.put("projectType", req.getProjectType());
        queryMap.put("status", req.getStatus());
        queryMap.put("plant", req.getPlant());
        queryMap.put("assetsNo", req.getAssetsNo());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("lowAmount", req.getLowAmount());
        queryMap.put("highAmount", req.getHighAmount());
        Map<String, Object> dataMap = assetsService.pageAssetsInfo(queryMap);
//        int totalNum = assetsService.getAssetsInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询固资信息成功");
        resp.setDataMap(dataMap);
//        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增固资信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增固资信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsInfo(@RequestBody UpdateAssetsReq req) {
        Assets assets = new Assets();
        assets.setAssetsNo(req.getAssetsNo());
        assets.setDepartment(req.getDepartment());
        assets.setProjectName(req.getProjectName());
        assets.setProjectManage(req.getProjectManage());
        assets.setProjectType(req.getProjectType());
        assets.setGreenAmount(req.getGreenAmount());
        assets.setCurrentYear(req.getCurrentYear());
        assets.setCurrentYearAmount(req.getCurrentYearAmount());
        assets.setCurrentYearForecast(req.getCurrentYearForecast());
        assets.setCurrentYearActual(req.getCurrentYearActual());
        assets.setApproveTime(req.getApproveTime());
        assets.setPlanCloseDate(req.getPlanCloseDate());
        assets.setPlant(req.getPlant());
        assets.setRemark(req.getRemark());
        assetsService.addAssetsInfo(assets, req.getSourceList());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资信息成功");
        return resp;
    }

    /**
     * 修改固资信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改固资信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsInfo(@RequestBody UpdateAssetsReq req) {
        Assets assets = new Assets();
        assets.setId(req.getId());
        assets.setAssetsNo(req.getAssetsNo());
        assets.setDepartment(req.getDepartment());
        assets.setProjectName(req.getProjectName());
        assets.setProjectManage(req.getProjectManage());
        assets.setProjectType(req.getProjectType());
        assets.setGreenAmount(req.getGreenAmount());
        assets.setCurrentYear(req.getCurrentYear());
        assets.setCurrentYearAmount(req.getCurrentYearAmount());
        assets.setCurrentYearForecast(req.getCurrentYearForecast());
        assets.setCurrentYearActual(req.getCurrentYearActual());
        assets.setApproveTime(req.getApproveTime());
        assets.setPlanCloseDate(req.getPlanCloseDate());
        assets.setPlant(req.getPlant());
        assets.setRemark(req.getRemark());
        assetsService.updateAssetsInfo(assets, req.getSourceList());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资信息成功");
        return resp;
    }

    /**
     * 删除固资信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除固资信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsInfo(@RequestBody BaseDeleteReq req) {
        assetsService.deleteAssetsInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除固资信息成功");
        return resp;
    }

    /**
     * 导出固资信息
     *
     * @param response response
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出固资信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportAssetsInfo(@RequestBody QueryAssetsReq req, HttpServletResponse response) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("year", req.getCurrentYear());
        queryMap.put("department", req.getDepartment());
        queryMap.put("projectManage", req.getProjectManage());
        queryMap.put("projectType", req.getProjectType());
        queryMap.put("status", req.getStatus());
        queryMap.put("plant", req.getPlant());
        queryMap.put("assetsNo", req.getAssetsNo());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("lowAmount", req.getLowAmount());
        queryMap.put("highAmount", req.getHighAmount());
        assetsService.exportAssetsInfo(queryMap, response);
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入固资数据", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importAssetsInfo(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = assetsService.importAssetsInfo((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入固资信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入固资数据", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddAssetsInfo(@RequestBody ImportAssetsReq req) {
        return assetsService.batchAddAssetsInfo(req.getDataList());
    }

    /**
     * 修改固资状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status-update")
    @LogAnnotation(value = "修改固资状态", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsInfoStatus(@RequestBody UpdateAssetsReq req) {
        assetsService.updateAssetsInfoStatus(req.getId(), req.getStatus());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资状态成功");
        return resp;
    }

    /**
     * 查询固资拓展信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("expand")
    public BaseResp queryAssetsExpandInfo(@RequestBody QueryAssetsForecastReq req) {
        Map<String, Object> dataMap = assetsService.queryAssetsExpandInfo(req.getId(),req.getChooseYear());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("查询固资拓展信息成功");
        return resp;
    }

    /**
     * 查询固资预算来源信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("source-page")
    public BaseResp pageAssetsSourceInfo(@RequestBody QueryBaseReq req) {
        List<Map<String, Object>> dataList = assetsService.pageAssetsSourceInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("查询固资预算来源信息成功");
        return resp;
    }

    /**
     * 新增固资预算来源信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("source-add")
    @LogAnnotation(value = "新增固资预算来源信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsSourceInfo(@RequestBody UpdateAssetsSourceReq req) {
        AssetsSourceRelation assetsSourceRelation = new AssetsSourceRelation();
        assetsSourceRelation.setAssetsId(req.getAssetsId());
        assetsSourceRelation.setSource(req.getSource());
        assetsSourceRelation.setBudgetId(req.getBudgetId());
        assetsSourceRelation.setPlanAmount(req.getPlanAmount());
        assetsSourceRelation.setRemark(req.getRemark());
        assetsService.addAssetsSourceInfo(assetsSourceRelation);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资预算来源信息成功");
        return resp;
    }

    /**
     * 修改固资预算来源信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("source-update")
    @LogAnnotation(value = "修改固资预算来源信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsSourceInfo(@RequestBody UpdateAssetsSourceReq req) {
        AssetsSourceRelation assetsSourceRelation = new AssetsSourceRelation();
        assetsSourceRelation.setId(req.getId());
        assetsSourceRelation.setAssetsId(req.getAssetsId());
        assetsSourceRelation.setSource(req.getSource());
        assetsSourceRelation.setBudgetId(req.getBudgetId());
        assetsSourceRelation.setPlanAmount(req.getPlanAmount());
        assetsSourceRelation.setRemark(req.getRemark());
        assetsService.updateAssetsSourceInfo(assetsSourceRelation);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资预算来源信息成功");
        return resp;
    }

    /**
     * 删除固资预算来源信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("source-delete")
    @LogAnnotation(value = "删除固资预算来源信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsSourceInfo(@RequestBody BaseDeleteReq req) {
        assetsService.deleteAssetsSourceInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除固资预算来源信息成功");
        return resp;
    }

    /**
     * 查询固资预测信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("forecast-page")
    public BaseResp pageAssetsForecastInfo(@RequestBody QueryAssetsForecastReq req) {
        Map<String, Object> resultMap = assetsService.pageAssetsForecastInfo(req.getId(), req.getChooseYear());
        BaseResp resp = new BaseResp();
//        resp.setDataList(dataList);
        resp.setDataMap(resultMap);
        resp.setRetMessage("查询固资预测信息成功");
        return resp;
    }

    /**
     * 修改固资预测信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("forecast-update")
    @LogAnnotation(value = "修改固资预测信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsForecastInfo(@RequestBody UpdateAssetsForecastReq req) {
        AssetsMonthDetail assetsMonthDetail = new AssetsMonthDetail();
        assetsMonthDetail.setId(req.getId());
        assetsMonthDetail.setBusinessId(req.getAssetsId());
        assetsMonthDetail.setInfoYear(req.getInfoYear());
        assetsMonthDetail.setFebAmount(req.getFebAmount());
        assetsMonthDetail.setJanAmount(req.getJanAmount());
        assetsMonthDetail.setMarAmount(req.getMarAmount());
        assetsMonthDetail.setAprAmount(req.getAprAmount());
        assetsMonthDetail.setMayAmount(req.getMayAmount());
        assetsMonthDetail.setJunAmount(req.getJunAmount());
        assetsMonthDetail.setJulAmount(req.getJulAmount());
        assetsMonthDetail.setAugAmount(req.getAugAmount());
        assetsMonthDetail.setSepAmount(req.getSepAmount());
        assetsMonthDetail.setOctAmount(req.getOctAmount());
        assetsMonthDetail.setNovAmount(req.getNovAmount());
        assetsMonthDetail.setDecAmount(req.getDecAmount());
        assetsService.updateAssetsForecastInfo(assetsMonthDetail, req.getOrderList(), req.getCostList());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资预测信息成功");
        return resp;
    }

    /**
     * 查询固资订单同步记录信息
     *
     * @return 成功标识
     */
    @PostMapping("sync-page")
    public BaseResp pageAssetsOrderSyncInfo(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        assetsOrderService.pageAssetsOrderSyncInfo(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询固资订单同步记录信息成功");
        return resp;
    }

    /**
     * 同步固资订单信息
     *
     * @return 成功标识
     */
    @PostMapping("order-sync")
    @LogAnnotation(value = "同步固资订单信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp syncAssetsOrderInfo(@RequestBody UpdateAssetsOrderReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startDate", req.getStartDate());
        queryParam.put("endDate", req.getEndDate());
        assetsOrderService.syncAssetsOrderInfo(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("同步固资订单信息成功");
        return resp;
    }

    /**
     * 查询固资订单信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("order-page")
    public BaseResp pageAssetsOrderInfo(@RequestBody QueryAssetsOrderReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("assetsId", req.getAssetsId());
        queryParam.put("currentYear", req.getCurrentYear());
        List<Map<String, Object>> dataList = assetsOrderService.pageAssetsOrderInfo(queryParam);
        int totalNum = assetsOrderService.getAssetsOrderCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询固资订单信息成功");
        return resp;
    }

    /**
     * 校正固资花费信息
     *
     * @return 成功标识
     */
    @GetMapping("cost-check")
    public BaseResp checkAssetsCost() {
        assetsService.checkAssetsCost();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("校正固资花费信息成功");
        return resp;
    }

    /**
     * 查询固资预测锁定信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("snapshot-page")
    public BaseResp pageAssetsLockInfo(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("assetsId", req.getId());
        List<Map<String, Object>> dataList = assetsLockService.pageAssetsLockInfo(queryParam);
        int totalNum = assetsLockService.getAssetsLockInfoCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询固资预测快照信息成功");
        return resp;
    }

    /**
     * 新增固资预测锁定信息
     *
     * @return 成功标识
     */
    @PostMapping("snapshot-add")
    public BaseResp addAssetsLock(@RequestBody UpdateAssetsLockReq req) {
        assetsLockService.addAssetsLockInfo(req.getConfirmFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增固资预测快照信息成功");
        return resp;
    }

    /**
     * 查询固资预测锁定信息
     *
     * @param req 请求信息
     * @return 固资预测锁定信息
     */
    @PostMapping("snapshot-get")
    public BaseResp getAssetsLockInfo(@RequestBody QueryBaseReq req) {
        List<Map<String,Object>> dataList = assetsLockService.getAssetsLockInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("查询固资预测锁定信息成功");
        return resp;
    }

    /**
     * 导出固资预测锁定信息
     *
     * @param response response
     * @return 导出数据
     */
    @PostMapping("snapshot-export")
    @LogAnnotation(value = "导出固资预测锁定信息", business = LogBusinessConstants.ASSETS_MANAGEMENT, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportAssetsLockInfo(@RequestBody QueryBaseReq req, HttpServletResponse response) {
        assetsLockService.exportAssetsLockInfo(req.getId(), response);
    }

    /**
     * 生成固资统计信息
     *
     * @return 成功标识
     */
    @GetMapping("statistics")
    public BaseResp generateAssetsStatistics() {
        assetsService.generateAssetsStatistics();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("生成固资统计信息成功");
        return resp;
    }

    /**
     * 修改固资等级信息
     *
     * @return 成功标识
     */
    @PostMapping("recommend")
    public BaseResp updateAssetsRecommend(@RequestBody QueryBaseReq req) {
        assetsService.updateAssetsRecommend(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改固资等级信息成功");
        return resp;
    }
}
